package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.AccountDao;
import com.edu.modules.baseinfo.dao.HomeWorkDao;
import com.edu.modules.baseinfo.dao.LessonDao;
import com.edu.modules.baseinfo.model.HomeWork;
import com.edu.modules.baseinfo.model.Student;
import com.edu.modules.baseinfo.vo.HomeWorkListVo;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.comm.WXData;
import com.edu.modules.entity.THomeworkEntity;
import com.edu.modules.entity.TLessonEntity;
import com.edu.modules.entity.TStudentHomeworkEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.FileUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class HomeWorkService extends BaseService {

	@Autowired
	private HomeWorkDao homeWorkDao;

	@Autowired
	private LessonDao lessonDao;

	@Autowired
	private AccountDao accountDao;


	public ResultVo saveTeacherHomeWork(JSONObject param){

		String msg = null;
		boolean flag = false;
		THomeworkEntity homwork = new THomeworkEntity();
		String homeworkId = IdGen.uuid();
		homwork.setHomeworkId(homeworkId);
		homwork.setLessonId(lessonDao.getLessonId(param.getString("teacherId"),param.getString("lessonName"),
				param.getString("lessonClass")));
		homwork.setTeacherId(param.getString("teacherId"));
		homwork.setHomeworkSubject(param.getString("subject"));
		homwork.setCreateTime(DateUtils.getDateTime());
		try {
			homeWorkDao.saveObject(homwork);
			homeWorkDao.saveStudentHomeworkUrl(homeworkId,param.getString("homworkUrl"),
					DateUtils.getDateTime(),"teacher");
			msg = "success";
			flag = true;
		}catch (Exception e){
			msg = "fail";
			flag = true;
		}
		return new ResultVo(flag,msg);
	}

	public ResultVo saveStudentHomework(String studentId,String homeworkId){

		TStudentHomeworkEntity studentHomework = new TStudentHomeworkEntity();
		studentHomework.setStudentHomeworkId(IdGen.uuid());
		studentHomework.setStudentId(studentId);
		studentHomework.setHomeworkId(homeworkId);
		studentHomework.setCreateTime(DateUtils.getDateTime());
		studentHomework.setUpdateTime(DateUtils.getDateTime());

		try {
			homeWorkDao.saveOrUpdate(studentHomework);
			return new ResultVo(true,"success");
		}catch (Exception e){
			e.printStackTrace();
			return new ResultVo(false,"fail");
		}
	}

	public HomeWorkListVo getStudentHomeworkListByCode(String code){

		Map student = accountDao.getStudentId(WXData.getOpenId(code));
		String studentId = student.get("student_id").toString();
		if(studentId==null){
			return new HomeWorkListVo(null,"未注册");
		}else{
			return new HomeWorkListVo(homeWorkDao.getStudentHomeworkList(studentId),"success");
		}
	}

	public HomeWorkListVo getStudentHomeworkList(String studentId){

		return new HomeWorkListVo(homeWorkDao.getStudentHomeworkList(studentId),"success");

	}

	public List getTitleUrlList(String studentId) {

		return homeWorkDao.getTitleUrlList(studentId);
	}

	public List getHomeworkStudentUrlList(String studentId,String homeworkId) {

		return homeWorkDao.getHomeworkStudentUrlList(studentId,homeworkId);
	}

	public List getReviedHomeworkUrlList(String studentId,String homeworkId) {

		return homeWorkDao.getReviedHomeworkUrlList(studentId,homeworkId);
	}

	public Object getHomeWorkDetail(String studentId,String homeworkId){

		return homeWorkDao.getHomeWorkDetile(studentId,homeworkId);
	}

	public JSONObject uploadHomeWork(MultipartFile file){

		JSONObject result= new JSONObject();
		if(!file.isEmpty()){
			try {
				String fileName = System.currentTimeMillis()+ file.getOriginalFilename();
				String filePath = "/opt/apache-tomcat-8.5.30/webapps/homework";
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath,
						fileName));
				result.put("fileName",fileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public JSONObject uploadTeacherReviewVoice(MultipartFile voice){

		JSONObject result= new JSONObject();
		if(!voice.isEmpty()){
			try {
				String fileName = System.currentTimeMillis() + voice.getOriginalFilename()+".wav";
				String filePath = "/opt/apache-tomcat-8.5.30/webapps/voice";
				FileUtils.copyInputStreamToFile(voice.getInputStream(), new File(filePath,
						fileName));
				result.put("fileName","/voice/"+fileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public JSONObject getTeacherHomeWorkByPage(JSONObject param) {

		int offset = param.getInteger("offset");
		int limit = param.getInteger("limit");

		String lessonName = param.getString("lessonName");
		String lessonClass = param.getString("lessonClass");
		String teacherName = param.getString("teacherName");

		List list = new ArrayList();
		list = homeWorkDao.getTeacherHomeWorkByPage(lessonName, lessonClass,
				teacherName, offset, limit);

		JSONObject result = new JSONObject();
		result.put("total", homeWorkDao.getTeacherHomeWorkCounts(lessonName,lessonClass,teacherName));
		result.put("rows", list);

		return result;
	}

	public JSONObject getStudentHomeWorkByPage(JSONObject param) {

		int offset = param.getInteger("offset");
		int limit = param.getInteger("limit");
		String studentName = param.getString("studentName");
		String lessonName = param.getString("lessonName");
		String lessonClass = param.getString("lessonClass");
		String teacherName = param.getString("teacherName");
		String reviewStatus = param.getString("reviewStatus");
		String lessonStartTime = param.getString("lessonStartTime");

		List list = new ArrayList();
		list = homeWorkDao.getStudentHomeWorkByPage(studentName,lessonName, lessonClass,teacherName,lessonStartTime,reviewStatus,offset, limit);

		JSONObject result = new JSONObject();
		result.put("total", homeWorkDao.getStudentHomeWorkCounts(studentName,lessonName, lessonClass,teacherName,lessonStartTime,reviewStatus));
		result.put("rows", list);

		return result;
	}

	public ResultVo deleteTeacherHomeWork(String homeWorkId){

		homeWorkDao.deleteObjects("t_homework","homework_id",homeWorkId);

		return new ResultVo(true,"success");
	}

	public ResultVo saveStudentHomework(JSONObject param){

		String studentId = param.getString("studentId");
		String homeworkId = param.getString("homeworkId");
		String imgUrl = param.getString("imgUrl");
		String date = DateUtils.getDateTime();
		String studentHomeworkId = IdGen.uuid();

		homeWorkDao.saveStudentHomework(studentHomeworkId,studentId,homeworkId,date);

		homeWorkDao.saveStudentHomeworkUrl(studentHomeworkId,imgUrl,date,"student");

		return new ResultVo(true,"success");
	}

	public ResultVo updateStudentHomeWork(JSONObject param){

		String studentHomeworkId = param.getString("studentHomeworkId");
		String score = param.getString("score");
		String comment = param.getString("comment");
		String reviewUrl = param.getString("reviewUrl");
		String url = param.getString("homeworkUrl");
		String studentId = param.getString("studentId");
		String homeworkId = param.getString("homeworkId");
		String updateTime = DateUtils.getDateTime();

		homeWorkDao.updateStudentHomeWork(studentHomeworkId,score,comment,reviewUrl,updateTime);

		homeWorkDao.saveReviewedHomeworkUrl(homeworkId,url,studentId);

		return new ResultVo(true,"success");
	}

	public JSONObject getStudentHomeworkUrl(String studentId,String homeworkId) {

		JSONObject result = new JSONObject();
		result.put("homeworkUrls",getHomeworkStudentUrlList(studentId,homeworkId));
		return result;
	}

	public JSONObject getTeacherHomeworkUrl(String homeworkId) {

		JSONObject result = new JSONObject();
		result.put("homeworkUrls",homeWorkDao.getTeacherHomeworkUrl(homeworkId));
		return result;
	}

	public ResultVo deleteHomeWork(String studentHomeworkId){

		homeWorkDao.deleteObjects("t_student_homework","student_homework_id","'"+studentHomeworkId+"'");

		return new ResultVo(true,"success");
	}
}
